package com.dh.project.controller;

import com.dh.project.model.*;
import com.dh.project.other.ModelRes;
import com.dh.project.service.LableInfoService;
import com.dh.project.service.OrderServiceProjectService;
import com.dh.project.service.ServiceProjectService;
import com.dh.project.utils.PageUtil;
import com.dh.project.utils.StringUtil;
import com.dh.project.view.MaintainListView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/4/17.
 */
@Controller
public class ProductController {

    private ServiceProjectService serviceProjectService;

    private LableInfoService lableInfoService;

    private OrderServiceProjectService orderServiceProjectService;

    @Autowired
    public ProductController(ServiceProjectService serviceProjectService,LableInfoService lableInfoService,
                             OrderServiceProjectService orderServiceProjectService){
        this.serviceProjectService = serviceProjectService;
        this.lableInfoService = lableInfoService;
        this.orderServiceProjectService = orderServiceProjectService;
    }

    /**
     * 产品内容列表
     * @param serviceProject
     * @param page
     * @param model
     * @return
     */
    @RequestMapping(value = "selectServiceProjectList",produces = "text/html;charset=utf-8")
    public String selectServiceProjectList(ServiceProject serviceProject, int page, Model model){
        //查询产品内容
        Map map = new HashMap();
        map.put("service_class",serviceProject.getService_class());//分类
        map.put("service_brand",serviceProject.getService_brand());//品牌
        map.put("service_kind",serviceProject.getService_kind());//品牌

        map.put("service_standard",serviceProject.getService_standard());//规格
        map.put("service_assembly",serviceProject.getService_assembly());//部件
        map.put("service_back",serviceProject.getService_back()); //型号

        map.put("page_start",(page-1)*10);
        map.put("page_end",10);

        List<ServiceProject> serviceProjects = serviceProjectService.selectServiceProjectListByMap(map);

        Map lableMap = new HashMap();
        //分类
        if (null != serviceProject.getService_class()){
            lableMap.put("lable_class_one",serviceProject.getService_class());
        }
        if (null != serviceProject.getService_kind()){
            if (serviceProject.getService_kind() != 0){
                lableMap.put("lable_class_two",serviceProject.getService_kind());
            }
        }
        //种类
//        if (serviceProject.getService_kind() != null){
//            lableMap.put("lable_class_two",serviceProject.getService_kind());
//        }
        //Lable部分内容
        List<LableInfo> lableInfos = lableInfoService.selectByMap(lableMap);

        model.addAttribute("lableInfos",lableInfos);

        //分页部分
        PageModel pageModel = serviceProjectService.selectServiceProjectListByCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,10);
        pageInfo.setPage_href(PageUtil.getPageHrefByFive(serviceProject,1));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),10));

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("serviceProject",serviceProject);
        model.addAttribute("serviceProjects",serviceProjects);
        return "service_project_list";
    }


    /**
     * 维修价格列表
     * @param model
     * @return
     */
    @RequestMapping(value = "selectMaintainList",produces = "text/html;charset=utf-8")
    public String selectMaintainList(MaintainListView maintainListView,int page, Model model){
        Map map = new HashMap();
        map.put("page_start",(page-1)*10);
        map.put("page_end",10);
        map.put("service_kind",maintainListView.getService_kind());
        map.put("max_price",maintainListView.getMax_price());
        map.put("min_price",maintainListView.getMin_price());
        List<MaintainListView> serviceProjects = serviceProjectService.selectMaintainListByMap(map);

        //分页部分
        PageModel pageModel = serviceProjectService.selectMaintainListByMapCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,10);
        pageInfo.setPage_href(PageUtil.getPageHrefThree(maintainListView,2));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),10));
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("serviceProjects",serviceProjects);
        model.addAttribute("maintainListView",maintainListView);
        return "maintain_list";
    }

    /**
     * 添加维修费
     * @param serviceProject
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "insertMaintain",produces = "text/html;charset=utf-8")
    public Object insertMaintain(ServiceProject serviceProject){

        serviceProject.setService_name(serviceProject.getService_content());
        serviceProject.setService_class(7);
        serviceProject.setService_brand(0);
        serviceProject.setService_standard(0);
        serviceProject.setService_assembly(0);
        serviceProject.setService_price(BigDecimal.valueOf(0.00));
        serviceProject.setMaintenance_cost(BigDecimal.valueOf(0.00));
        serviceProject.setCreatetime(String.valueOf(new Date().getTime()));
        serviceProject.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = serviceProjectService.insert(serviceProject);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"添加失败!");
        }
    }


    /**
     * 修改维修费内容
     * @param serviceProject
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateMaintain",produces = "text/html;charset=utf-8")
    public Object updateMaintain(ServiceProject serviceProject){
        ServiceProject serviceProject1 = serviceProjectService.selectById(serviceProject.getService_id());

        if (serviceProject.getService_kind() != null){
            serviceProject1.setService_kind(serviceProject.getService_kind());
        }

        //修改价格
        if (serviceProject.getService_all_price() != null){
            serviceProject1.setService_all_price(serviceProject.getService_all_price());
        }
        //修改名称
        if (!StringUtil.isBlank(serviceProject.getService_content())){
            serviceProject1.setService_content(serviceProject.getService_content());
        }
        //修改备注
        if (!StringUtil.isBlank(serviceProject.getService_back())){
            serviceProject1.setService_back(serviceProject.getService_back());
        }
        //修改时间
        serviceProject1.setModifytime(String.valueOf(new Date().getTime()));

        boolean isSuccess = serviceProjectService.updateById(serviceProject1);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"修改失败!");
        }
    }

    /**
     * 删除维修费内容
     * @param admin_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteProject",produces = "text/html;charset=utf-8")
    public Object deleteProject(int admin_id){
        Map map = new HashMap();
        map.put("service_id",admin_id);
        List<OrderServiceProject> orderServiceProjects = orderServiceProjectService.selectByMap(map);
        if (orderServiceProjects.size() > 0){
            return new ModelRes(ModelRes.Status.ERROR,"该服务项已有订单在使用，删除失败!");
        }
        boolean isSuccess = serviceProjectService.deleteById(admin_id);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"删除失败!");
        }
    }

    /**
     * 添加产品 初始化页面数据
     * @param serviceProject
     * @param model
     * @return
     */
    @RequestMapping(value = "addServiceProjectInit",produces = "text/html;charset=utf-8")
    public String addServiceProjectInit(ServiceProject serviceProject, Model model){
        Map lableMap = new HashMap();
        //分类
        if (null != serviceProject.getService_class() ){
            lableMap.put("lable_class_one",serviceProject.getService_class());
        }
        if (null != serviceProject.getService_kind()){
            if ( serviceProject.getService_kind() != 0){
                lableMap.put("lable_class_two",serviceProject.getService_kind());
            }
        }
        //Lable部分内容
        List<LableInfo> lableInfos = lableInfoService.selectByMap(lableMap);

        model.addAttribute("lableInfos",lableInfos);
        model.addAttribute("serviceProject",serviceProject);
        return "add_service_project";
    }


    /**
     * 添加产品
     * @param serviceProject
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "InsertServiceProject",produces = "text/html;charset=utf-8")
    public Object InsertServiceProject(ServiceProject serviceProject){
        //产品总价
        double allPrice = serviceProject.getService_price().doubleValue()+serviceProject.getMaintenance_cost().doubleValue();
        serviceProject.setService_all_price(BigDecimal.valueOf(allPrice));
        serviceProject.setCreatetime(String.valueOf(new Date().getTime()));
        serviceProject.setModifytime(String.valueOf(new Date().getTime()));
        serviceProject.setService_brand_name(lableInfoService.selectById(serviceProject.getService_brand()).getLable_name());
        serviceProject.setService_standard_name(lableInfoService.selectById(serviceProject.getService_standard()).getLable_name());
        serviceProject.setService_assembly_name(lableInfoService.selectById(serviceProject.getService_assembly()).getLable_name());
        boolean isSuccess = serviceProjectService.insert(serviceProject);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"添加失败！");
        }
    }


    /**
     * 根据id查询产品信息
     * @param serviceProject
     * @param model
     * @return
     */
    @RequestMapping(value = "selectServiceProjectById",produces = "text/html;charset=utf-8")
    public String selectServiceProjectById(ServiceProject serviceProject,Model model){
        ServiceProject serviceProject1 = serviceProjectService.selectById(serviceProject.getService_id());
        model.addAttribute("serviceProject",serviceProject1);


        Map lableMap = new HashMap();
        //分类
        if (serviceProject1.getService_class() != null){
            lableMap.put("lable_class_one",serviceProject1.getService_class());
        }
        //Lable部分内容
        List<LableInfo> lableInfos = lableInfoService.selectByMap(lableMap);
        model.addAttribute("lableInfos",lableInfos);
        return "service_project_detail";
    }

    /**
     * 修改产品
     * @param serviceProject
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateServiceProject",produces = "text/html;charset=utf-8")
    public Object updateServiceProject(ServiceProject serviceProject){
        double allPrice = serviceProject.getService_price().doubleValue()+serviceProject.getMaintenance_cost().doubleValue();
        serviceProject.setService_all_price(BigDecimal.valueOf(allPrice));
        serviceProject.setService_brand_name(lableInfoService.selectById(serviceProject.getService_brand()).getLable_name());
        serviceProject.setService_standard_name(lableInfoService.selectById(serviceProject.getService_standard()).getLable_name());
        serviceProject.setService_assembly_name(lableInfoService.selectById(serviceProject.getService_assembly()).getLable_name());
        serviceProject.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = serviceProjectService.updateById(serviceProject);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"修改失败！");
        }
    }


    /**
     * 订单添加产品页面
     * @param serviceProject
     * @param page
     * @param model
     * @return
     */
    @RequestMapping(value = "OrderAddServiceProject",produces = "text/html;charset=utf-8")
    public String OrderAddServiceProject(ServiceProject serviceProject,int order_id, int page, Model model){
        //查询产品内容
        Map map = new HashMap();
        map.put("service_class",serviceProject.getService_class());//分类
        map.put("service_brand",serviceProject.getService_brand());//品牌
        map.put("service_kind",serviceProject.getService_kind());//品牌

        map.put("service_standard",serviceProject.getService_standard());//规格
        map.put("service_assembly",serviceProject.getService_assembly());//部件
        map.put("service_back",serviceProject.getService_back()); //型号

        map.put("page_start",(page-1)*10);
        map.put("page_end",10);

        List<ServiceProject> serviceProjects = serviceProjectService.selectServiceProjectListByMap(map);

        Map lableMap = new HashMap();
        //分类
        if (null != serviceProject.getService_class()){
            lableMap.put("lable_class_one",serviceProject.getService_class());
        }
        if (null != serviceProject.getService_kind()){
            if (serviceProject.getService_kind() != 0){
                lableMap.put("lable_class_two",serviceProject.getService_kind());
            }
        }
        //种类
//        if (serviceProject.getService_kind() != null){
//            lableMap.put("lable_class_two",serviceProject.getService_kind());
//        }
        //Lable部分内容
        List<LableInfo> lableInfos = lableInfoService.selectByMap(lableMap);

        model.addAttribute("lableInfos",lableInfos);

        //分页部分
        PageModel pageModel = serviceProjectService.selectServiceProjectListByCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,10);
        pageInfo.setPage_href(PageUtil.getPageHrefByFive(serviceProject,3));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),10));

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("serviceProject",serviceProject);
        model.addAttribute("serviceProjects",serviceProjects);
        model.addAttribute("order_id",order_id);
        return "order_add_service_project";
    }
}
